﻿define(['angular'], function (angular) {

    /*-----------------------------------------
     *   demo serice
     ------------------------------------------*/
    angular.module('books.service.borrower', [])
        .constant('borrowerServiceConfig', {
            files: {//调用json数据
                query: 'app/data/borrower/query.json',
                get: 'app/data/borrower/get.json',
                save: 'app/data/borrower/save.json',
                update: 'app/data/borrower/save.json',
                remove: 'app/data/borrower/remove.json'
            },
            urls: {
                query: 'http://47.92.95.246:8012/getborrowerlist',//调用后台api接口
                get: 'http://47.92.95.246:8012/getborrowerbyid',
                save: 'http://47.92.95.246:8012/saveborrower',
                update: 'http://47.92.95.246:8012/updateborrower',
                remove: 'http://47.92.95.246:8012/deleteborrower'
            }
        })
        .factory('borrowerService', ['$http', '$q', 'borrowerServiceConfig', function ($http, $q, borrowerServiceConfig) {

            return {
                /**
                 * 查询tableList 列表
                 * 
                 * @return {[type]} [description]
                 */
                query: function (params, queryParams) {
                    //内置服务 同步请求数据
                    var deffered = $q.defer();
                    var q = {
                        borrowerName: queryParams.borrowerName
                    };
                    q.pageNow = params.page();
                    q.pageSize = params.count();
                    //请求数据
                    $http({
                        method: 'GET',
                        dataType: "json",
                        contentType: 'application/json; charset=UTF-8',
                        params: q,
                        url: borrowerServiceConfig.urls.query
                    })
                    //请求数据
                    //$http.post(borrowerServiceConfig.urls.query, q,
                    //    {
                    //        'Content-Type': 'application/x-www-form-urlencoded'
                    //    }
                    //)
                    .success(function (data, status, config, headers) {
                        params.total(data.data.total);
                        deffered.resolve(data);
                    })
                    .error(function (data, status, config, headers) {
                        deffered.reject(status);
                    });
                    return deffered.promise;
                },

                /**
                 * 获取单条记录
                 * 
                 * @return {type} 
                 */
                get: function (id) {
                    //内置服务 同步请求数据
                    var deffered = $q.defer();
                    //请求数据
                    $http({
                        method: 'POST',
                        dataType: "json",
                        contentType: 'application/json; charset=UTF-8',
                        params: { id: id },
                        url: borrowerServiceConfig.urls.get
                    })
                    .success(function (response) {
                        deffered.resolve(response);
                    })
                    .error(function (e, code) {
                        deffered.reject(code);
                    });
                    return deffered.promise;
                },
                /**
                 * 保存单条记录
                 * 
                 * @return {[type]} [description]
                 */
                save: function (saveJson) {
                    //内置服务 同步请求数据
                    var deffered = $q.defer();

                    //请求数据
                    $http({
                        method: 'POST',
                        dataType: "json",
                        contentType: 'application/json; charset=UTF-8',
                        url: borrowerServiceConfig.urls.save,
                        params: saveJson
                    })
                    .success(function (response) {
                        deffered.resolve(response);
                    })
                    .error(function (e, code) {
                        deffered.reject(code);
                    });
                    return deffered.promise;
                },
                /**
                 * 保存单条记录
                 * 
                 * @return {[type]} [description]
                 */
                update: function (saveJson) {
                    //内置服务 同步请求数据
                    var deffered = $q.defer();
                    var q = {
                        id: saveJson.id,
                        borrowerName: saveJson.borrowerName,
                        gender: saveJson.gender,
                        cellphone: saveJson.cellphone,
                        email: saveJson.email,
                        relationship: saveJson.relationship,
                        record: saveJson.record
                    };
                    //请求数据
                    $http({
                        method: 'POST',
                        dataType: "json",
                        contentType: 'application/json; charset=UTF-8',
                        url: borrowerServiceConfig.urls.update,
                        params: q
                    })
                    .success(function (response) {
                        deffered.resolve(response);
                    })
                    .error(function (e, code) {
                        deffered.reject(code);
                    });
                    return deffered.promise;
                },
                remove: function (id) {
                    //内置服务 同步请求数据
                    var deffered = $q.defer();
                    //请求数据
                    $http({
                        method: 'POST',
                        dataType: "json",
                        contentType: 'application/json; charset=UTF-8',
                        url: borrowerServiceConfig.urls.remove,
                        params: {
                            id: id
                        }
                    })
                    .success(function (response) {
                        deffered.resolve(response);
                    })
                    .error(function (e, code) {
                        deffered.reject(code);
                    });
                    return deffered.promise;
                },

                /**
                 * 根据系统代码类型（sys_code），返回代码值、代码名，并按顺序号排序 
                 * 一般用于查询下拉框的公共方法~ 暂时没用
                 * 
                 * @param codeType
                 * @param showID
                 * @return
                 */
                retrSysCodes: function (codeType, showID) {
                    var deffered = $q.defer();//内置服务 同步请求数据
                    //请求数据
                    $http({
                        // method:'POST',
                        dataType: "json",
                        contentType: 'application/json; charset=UTF-8',
                        url: systemServiceConfig.urls.retrSysCodes,
                        params: {
                            codeType: codeType,
                            showID: showID
                        }
                    })
                    .success(function (response) {
                        deffered.resolve(response);
                    })
                    .error(function (e, code) {
                        deffered.reject(code);
                    });
                    return deffered.promise;
                }

            };
        }]);

});
